<template>
  <div class="login">
    <van-nav-bar>
      <template #left>
        <i class="iconfont iconbtn_nav_back myiconleft"></i>
      </template>
    </van-nav-bar>
    <!-- //内容 -->
    <div class="conter">
      <h2>你好，先登录</h2>
      <van-form ref="myform">
        <van-field
          v-model="user.mobile"
          name="mobile"
          :rules="rules.mobile"
          placeholder="请输入手机号"
          ><template #left-icon> <i class="iconfont iconbianzu1"></i> </template
        ></van-field>

        <van-field
          v-model="user.code"
          placeholder="请输入验证码"
          :rules="rules.code"
        >
          <template #left-icon>
            <i class="iconfont iconyanzhengma"></i>
          </template>
          <template #button>
            <span @click="getcode" class="mycolor">{{
              isback ? time + 's后重新获取' : '获取验证码'
            }}</span>
          </template></van-field
        >
        <p class="word">
          登陆即同意<span class="mycolor">《用户使用协议》</span>
          和
          <span class="mycolor">《隐私协议》</span>
        </p>
        <div class="loginbtn">
          <van-button round block type="danger" native-type="submit">
            确定
          </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { apiGetcode } from '@/api/au.js'
export default {
  data () {
    return {
      isback: false,
      time: 5,
      timer: null,
      user: {
        mobile: '18811111111',
        code: ''
      },
      rules: {
        mobile: [
          { required: true, message: '请填写手机号', trigger: 'onBlur' },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请填写正确手机号',
            trigger: 'onBlur'
          }
        ],
        code: [
          { required: true, message: '请填写验证码', trigger: 'onBlur' },
          {
            pattern: /^\d{4}$/,
            message: '请填写正确验证码',
            trigger: 'onBlur'
          }
        ]
      }
    }
  },
  methods: {
    async getcode () {
      if (this.isback) {
        return
      }
      this.isback = true
      this.timer = setInterval(() => {
        this.time--
        if (this.time < 0) {
          clearInterval(this.timer)
          this.isback = false
          this.time = 5
        }
      }, 1000)
      await this.$refs.myform.validate('mobile')
      this.$toast.loading({
        duration: 0,
        forbidClick: true,
        message: '加载中'
      })
      const rescode = await apiGetcode(this.user.mobile)
      console.log(rescode)
      this.$toast.success(rescode.data)
    }
  }
}
</script>

<style lang="less" scope>
.login {
  font-size: 12px;
  .myiconleft {
    font-size: 40px;
  }
  /deep/ .van-nav-bar__left {
    padding: 0;
  }
  .conter {
    padding: 15px;
    h2 {
      margin-top: 50px;
      margin-bottom: 60px;
    }
  }
  .mycolor {
    color: #16bed8;
  }
  .word {
    margin-top: 14px;
    margin-bottom: 15px;
  }
  .loginbtn {
    margin-top: 44px;
  }
}
</style>
